package com.qyhl.cloud.webtv.module_integral.wallet.cash;

import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.PathConfigConstant;
import com.qyhl.webtv.commonlib.entity.intergral.AccountBean;
import com.qyhl.webtv.commonlib.entity.intergral.CashLimitBean;
import com.qyhl.webtv.commonlib.entity.intergral.IntegralUserInfoBean;
import com.qyhl.cloud.webtv.module_integral.common.IntergralUrl;
import com.qyhl.cloud.webtv.module_integral.common.IntergralUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

public class CashModel implements CashContract.CashModel {
    private CashPresenter mPresenter;

    CashModel(CashPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getCashLimit() {
        Map<String, String> map = DESedeUtil.encodeMapUrl(IntergralUrl.INTEGRAL_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.version);
            object.put("name", "cashLimit");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<CashLimitBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getError(e.getCode(), IntergralUtils.getInstance().getScoreMessage(e.getCode()));
                    }

                    @Override
                    public void onSuccess(CashLimitBean cashLimitBean) {
                        mPresenter.getSuccess(cashLimitBean);
                    }
                });
    }

    @Override
    public void doCommit(String username, int money, String platform) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(IntergralUrl.INTEGRAL_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.version);
            object.put("name", "applyCash");
            object.put("money", money);
            object.put("platform", platform);
            object.put("username", username);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<AccountBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.docommitError(e.getCode(), IntergralUtils.getInstance().getScoreMessage(e.getCode()));
                    }

                    @Override
                    public void onSuccess(AccountBean accountBean) {
                        mPresenter.docommitSuccess(accountBean);
                    }
                });

    }

    @Override
    public void getInfo(String username) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(IntergralUrl.INTEGRAL_URL);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name", "userInfo");
            jsonObject.put("version", PathConfigConstant.version);
            jsonObject.put("username", username);
            jsonObject.put("siteId", CommonUtils.getInstance().getSite());
        } catch (JSONException e) {
            e.printStackTrace();
        }


        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY),
                        jsonObject.toString()
                ))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<IntegralUserInfoBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getInfoError(IntergralUtils.getInstance().getScoreMessage(e.getCode()));
                    }

                    @Override
                    public void onSuccess(IntegralUserInfoBean integralUserInfoBean) {
                        mPresenter.getInfoSuccess(integralUserInfoBean);
                    }
                });
    }
}
